package com.waypolice.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.waypolice.manager.service.ArchiveDetailStatisticsService;
import com.waypolice.manager.service.SinArcStatisticsService;
import com.waypolice.mapper.CmsSinItemMapper;
import com.waypolice.mapper.handwriting.ArchiveDetailStatisticsMapperSon;
import com.waypolice.mapper.handwriting.SinArcStatisticsMapperSon;
import com.waypolice.pojo.CmsSinItem;
import com.waypolice.pojo.CmsSinItemExample;
import com.waypolice.pojo.handwriting.WriteSql;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 312小时 on 2019/4/1.
 */
@Service
public class SinArcStatisticsServiceImpl implements SinArcStatisticsService {

    @Autowired
    private SinArcStatisticsMapperSon sinArcStatisticsMapperSon;


    @Override
    public Object getSinArchiveDetailStatistics(int offset, int limit) {
        Map<String, Object> result = new HashMap<>();
        String str="SELECT COUNT (1) AS SINITEM,ITEM_ID,ITEM_NAME,ITEM_TITLE,ITEM_RESPONS,ITEM_STR1_DATE,ITEM_PAGE,ITEM_NUM,ITEM_EXP3,ITEM_EXP2,ITEM_SECRET\n" +
                "FROM CMS_SIN_ITEM\n" +
                "WHERE ITEM_ARC_STATUS = '1'\n" +
                "GROUP BY ITEM_ID,ITEM_NAME,ITEM_TITLE,ITEM_RESPONS,ITEM_STR1_DATE,ITEM_PAGE,ITEM_NUM,ITEM_EXP3,ITEM_EXP2,ITEM_SECRET";
        WriteSql sql=new WriteSql(str);
        List<Object> pageData = sinArcStatisticsMapperSon.getSinArcPageData(sql);
        PageHelper.offsetPage(offset, limit, false);
        List<Object> statisticsData = sinArcStatisticsMapperSon.getSinArcStatisticsData(sql);
        result.put("rows", statisticsData);
        result.put("total", pageData.size());
        return result;
    }

    @Override
    public Object getSinPostArchiveDetailStatistics(int offset, int limit, String vol_archive_unit, String vol_archive_year, String vol_storage_time, String vol_secret, String starttime, String endtime) {
        String ext=" AND 1=1 ";
        if(vol_archive_unit!=null&&vol_archive_unit!=""){
            ext+=" AND ITEM_EXP2 LIKE '%"+vol_archive_unit+"%' ";
        }
        if(vol_archive_year!=null&&vol_archive_year!=""){
            ext+=" AND ITEM_EXP3 LIKE '%"+vol_archive_year+"%' ";
        }
        if(vol_storage_time!=null&&vol_storage_time!=""){
            ext+=" AND ITEM_NUM LIKE '%"+vol_storage_time+"%' ";
        }
        if(vol_secret!=null&&vol_secret!=""){
            ext+=" AND ITEM_SECRET LIKE '%"+vol_secret+"%' ";
        }
        if(starttime==null||starttime==""){
            if(endtime!=null&&endtime!=""){
                ext+=" AND ITEM_STR1_DATE LIKE '%"+endtime+"%' ";
            }
        }else{
            if(endtime!=null&&endtime!=""){
                ext+=" AND ITEM_STR1_DATE >=  to_date('"+starttime+"','yyyy-MM-dd') AND ITEM_STR1_DATE <=to_date('"+endtime+"','yyyy-MM-dd') ";
            }else{
                ext+=" AND ITEM_STR1_DATE LIKE '%"+starttime+"%' ";
            }
        }
        Map<String, Object> result = new HashMap<>();
        String str=" SELECT COUNT (1) AS SINITEM,ITEM_ID,ITEM_NAME,ITEM_TITLE,ITEM_RESPONS,ITEM_STR1_DATE,ITEM_PAGE,ITEM_NUM,ITEM_EXP3,ITEM_EXP2,ITEM_SECRET\n" +
                "FROM CMS_SIN_ITEM\n" +
                "WHERE ITEM_ARC_STATUS = '1'\n" +ext+
                "GROUP BY ITEM_ID,ITEM_NAME,ITEM_TITLE,ITEM_RESPONS,ITEM_STR1_DATE,ITEM_PAGE,ITEM_NUM,ITEM_EXP3,ITEM_EXP2,ITEM_SECRET";
        WriteSql sql=new WriteSql(str);
        List<Object> pageData = sinArcStatisticsMapperSon.getSinArcPageData(sql);
        PageHelper.offsetPage(offset, limit, false);
        List<Object> statisticsData = sinArcStatisticsMapperSon.getSinArcStatisticsData(sql);
        result.put("rows", statisticsData);
        result.put("total", pageData.size());
        return result;
    }
}
